﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;

namespace Directory.Web
{
	/// <summary>
	/// ItemDateProxy 的摘要说明
	/// </summary>
	public sealed class ItemDataProxy
	{
		private ItemDataProxy() { }

		public static void CreateItem(int categoryId, string title, string keyWords, string description, string linkUrl, string logoPath, string authorName,
			string checkerName, int topState, bool IsRecommand, bool isChecked, out Directory.Model.ItemInfo.ErrorCode ErrorCode)
		{
			Directory.BLL.Item bll = new Directory.BLL.Item();
			bll.CreateItem(categoryId, title, keyWords, HttpContext.Current.Server.HtmlEncode(description), linkUrl, logoPath, authorName, checkerName, topState, IsRecommand, isChecked, out ErrorCode);
		}

		public static Directory.Model.ItemInfo GetItemInfo(int itemId)
		{
			Directory.BLL.Item bll = new Directory.BLL.Item();
			return bll.GetItemInfo(itemId);
		}

		public static void UpdateItemInfo(Directory.Model.ItemInfo item, out Directory.Model.ItemInfo.ErrorCode errorCode)
		{
			Directory.BLL.Item bll = new Directory.BLL.Item();
			item.Description = HttpContext.Current.Server.HtmlEncode(item.Description);
			bll.UpdateItem(item, out errorCode);
		}

		public static IList<Directory.Model.ItemInfo> GetItemsInCategory(int categoryId, int pageIndex, int pageSize, out int totalRecords)
		{
			Directory.BLL.Item bll = new Directory.BLL.Item();
			return bll.GetItemsInCategory(categoryId, true, pageIndex, pageSize, out totalRecords);
		}

		public static IList<Directory.Model.ItemInfo> GetUncheckedItemsInCategory(int categoryId, int pageIndex, int pageSize, out int totalRecords)
		{
			Directory.BLL.Item bll = new Directory.BLL.Item();
			return bll.GetItemsInCategory(categoryId, false, pageIndex, pageSize, out totalRecords);
		}

		public static void DeleteItem(int itemId)
		{
			Directory.BLL.Item bll = new Directory.BLL.Item();
			bll.DeleteItem(itemId);
		}

		public static void UpdateRecommandState(int categoryId, bool IsRecommand)
		{
			Directory.BLL.Item bll = new Directory.BLL.Item();
			bll.UpdateRecommandState(categoryId, IsRecommand);
		}

		public static IList<Directory.Model.ItemInfo> SearchItems(int categoryId, string keywords, int pageIndex, int pageSize, out int totalRecords)
		{
			Directory.BLL.Item bll = new Directory.BLL.Item();
			return bll.SearchItems(categoryId, keywords, pageIndex, pageSize, out totalRecords);
		}

		public static DataSet GetContributionRank(int pageIndex, int pageSize, out int totalRecords)
		{
			Directory.BLL.Item bll = new Directory.BLL.Item();
			return bll.GetContributionRank(pageIndex, pageSize, out totalRecords);
		}

		public static void UpdateCheckState(int itemId, bool isChecked)
		{
			Directory.BLL.Item bll = new Directory.BLL.Item();
			bll.UpdateCheckState(itemId, isChecked);
		}

		public static IList<Directory.Model.ItemInfo> GetRecommandItems(int pageIndex, int pageSize, out int totalRecords)
		{
			Directory.BLL.Item bll = new Directory.BLL.Item();
			return bll.GetRecommandItems(pageIndex, pageSize, out totalRecords);
		}

		public static IList<Directory.Model.ItemInfo> GetRecentAdded()
		{
			Directory.BLL.Item bll = new Directory.BLL.Item();
			return bll.GetRecentAdded(WebSettings.GetInstance().RecentAddedItemsCount);
		}

		public static int GetItemsCountInCategory(int categoryId, bool ischecked)
		{
			Directory.BLL.Item bll = new Directory.BLL.Item();
			return bll.GetItemsCountInCategory(categoryId, ischecked);
		}

		public static int GetReleasedItemsCount()
		{
			Directory.BLL.Item bll = new Directory.BLL.Item();
			return bll.GetItemsCount(true);
		}

		public static IList<Directory.Model.ItemInfo> GetUncheckedItems4User(string username, int pageIndex, int pageSize, out int totalRecords)
		{
			IList<Directory.Model.CategoryInfo> catList = CategoryDataProxy.GetRootCategoriesForUser(username);
			if (catList == null || catList.Count == 0)
			{
				totalRecords = 0;
				return new List<Directory.Model.ItemInfo>();
			}
			int[] arrId = new int[catList.Count];
			int i = 0;
			foreach (Directory.Model.CategoryInfo cat in catList)
			{
				arrId[i] = cat.CategoryId;
				i++;
			}
			Directory.BLL.Item bll = new Directory.BLL.Item();
			return bll.GetItemsInCategories(arrId, false, true, pageIndex, pageSize, out totalRecords);
		}
	}
}
